package com.hutra.utils {
	/**
	 * @author hunglx
	 */
	public class Dater{
		
		public static var unequal:Number = 0;
		public static var mid_unequal:Number = 0;
		public static var offset:Number = 0;
		public static var arrAVG:Array = new Array();
		
		public function Dater() {
			
		}
		
		/**
		 * 
		 * @param	timeclientsender
		 * @param	timeserversender
		 */
		public static function setOffset(timeclientsender:Number,timeserversender:Number):void
		{
			var thistime:Date = new Date  ;
			var ping:Number = thistime.time - timeclientsender;
			var latency:Number = ping / 2;
			unequal = timeserversender - thistime.time + latency;
			arrAVG.push(unequal);
			if (arrAVG.length >= 20)
			{
				var total:Number= new Number();
				for (var i:int=0; i<arrAVG.length; i++) total +=  arrAVG[i];
				unequal = Math.round(total/arrAVG.length);
				arrAVG = new Array();
			}
			if (mid_unequal == 0)
				{
					mid_unequal = unequal;
				}
				if (mid_unequal > unequal)
				{
					mid_unequal = unequal;
				}
				unequal = mid_unequal;
			offset = unequal;
		}
		public static function getOffset():Number
		{
			return offset;
		}
		public static function getTime():Number
		{
			var thistime:Date = new Date  ;
			return thistime.time + offset;
		}
		/**
		 * 
		 * @param	value
		 * @param	isfull
		 * @return
		 */
		public function toTimeString(value:Number,isfull:Boolean=true):String{
            var datestring:String = "";
			var day:Number 		= Math.floor((value / 86400));
            var hour:Number 	= Math.floor((value - day * 86400) / 3600);
            var minute:Number 	= Math.floor((value - day * 86400 - hour * 3600) / 60);
            var second:Number 	= Math.floor((value - day * 86400 - hour * 3600 - minute * 60));
            var k:Number;
			isfull = true;
			if (day){
                datestring = datestring + (String(day) + "d");
                if (isfull){
                    if (hour) {
						if (day) {
							k = hour + (day * 24);
							datestring = String(k) + "h";
						}else{
							//datestring = datestring + (" " + String(hour) + "h");
							datestring = String(hour) + "h";
						}
					}else {
						if (day) {
							k = hour + (day * 24);
							datestring = String(k) + "h";
						}
					}
                }else if (hour){
                    hour = hour + day * 24;
                    datestring = String(hour) + "h";
                }
            }else if (!day && hour){
                //datestring = datestring + (String(hour) + "h");
                datestring = String(hour) + "h";
                if (isfull && minute)
                    datestring = datestring + (" " + String(minute) + "m");
            }else if (!day && !hour && minute){
                datestring = datestring + (String(minute) + "m");
                if (isfull && second)
                    datestring = datestring + (" " + String(second) + "s");
            }else if (!day && !hour && !minute){
                datestring = datestring + (String(second) + "s");
            }
            return datestring;
        }
		/**
		 * 
		 * @param	value
		 * @param	isfull
		 * @return
		 */		
		public function toTimeStringLess(value:Number,isfull:Boolean=true):String {
            var datestring:String = "";
			var day:Number 		= Math.floor((value / 86400));
            var hour:Number 	= Math.floor((value - day * 86400) / 3600);
            var minute:Number 	= Math.floor((value - day * 86400 - hour * 3600) / 60);
            var second:Number 	= Math.floor((value - day * 86400 - hour * 3600 - minute * 60));
            
			datestring = datestring 
				+ ((day)?(String(day) + " d"):"")
				+ ((hour)?(", " + String(hour) + " h"):"")
				+ ((minute)?(", " + String(minute) + " m"):"")
				+ ((second)?(", " + String(second) + "'"):"");
				
            return datestring;
        }
		/**
		 * 
		 * @param	obj1
		 * @return
		 */
		public function humanReadableTime(obj1:Number) : String{
            var value2:* = Math.floor(obj1 / 1000);
            var value3:String = "";
            if (value2 < 0){
                value2 = Math.abs(value2);
                value3 = "-";
            }
            var value4:* = Math.floor(value2 / 60);
            if (Math.floor(value2 / 60) > 0){
                value2 = value2 - value4 * 60;
            }
            var value5:* = Math.floor(value4 / 60);
            if (Math.floor(value4 / 60) > 0){
                value4 = value4 - 60 * value5;
            }
            return value3 + (value5 < 10 ? ("0" + value5) : (value5)) + ":" + (value4 < 10 ? ("0" + value4) : (value4)) + ":" + (value2 < 10 ? ("0" + value2) : (value2));
        }
	}
}